const express = require('express');
const router = express.Router();
const foodModel = require('../db/model/foodModel');
/**
 * @api {post} /food/add 添加菜品
 * @apiName addfood
 * @apiGroup Food
 *
 * @apiParam {String} us 用户名
 * @apiParam {String} price 价格
 * @apiParam {String} typename 类型名称
 * @apiParam {String} typeid 类型id
 * @apiParam {String} img 图片
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/add', async (req, res) => {
  try {
    let { name, price, desc, typename, typeid, img } = req.body;
    if (!name || !price || !typeid) {
      return res.send({ code: 200, msg: '缺少参数' });
    }
    await foodModel.insertMany({ name, price, desc, typename, typeid, img });
    res.send({ code: 200, msg: '添加成功' });
  } catch (e) {
    res.send({ code: 200, msg: '添加失败' });
  }
});

/**
 * @api {post} /food/getInfoByType 分类查询
 * @apiName getInfoByType
 * @apiGroup Food
 *
 * @apiParam {String} typeid 类型id
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/getInfoByType', async (req, res) => {
  try {
    let { typeid } = req.body;
    if (!typeid) {
      return res.send({ code: 200, msg: '缺少参数' });
    }
    typeid = String(+typeid); // 把空格给trim了
    let data = await foodModel.find({ typeid });
    res.send({ code: 200, msg: '查询成功', list: data });
  } catch (e) {
    res.send({ code: 200, msg: '查询失败' });
  }
});

/**
 * @api {post} /food/getInfoBykw 关键字查询
 * @apiName getInfoBykw
 * @apiGroup Food
 *
 * @apiParam {String} kw 关键字
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/getInfoBykw', async (req, res) => {
  try {
    let { kw } = req.body;
    // let kw = '鱼';
    // console.log(kw);
    if (!kw) {
      return res.send({ code: 200, msg: '缺少参数' });
    }

    let reg = new RegExp(kw);
    let data = await foodModel.find({
      $or: [{ name: { $regex: reg } }, { desc: { $regex: reg } }],
    });
    res.send({ code: 200, msg: '查询成功', list: data });
  } catch (e) {
    res.send({ code: 200, msg: '查询失败' });
  }
});

/**
 * @api {post} /food/del 删除
 * @apiName del
 * @apiGroup Food
 *
 * @apiParam {String} _id id
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */

router.post('/del', async (req, res) => {
  try {
    let { _id } = req.body;
    // let ids = [..._id];// 都不用这么写..直接_id就是一个数组
    console.log(_id);
    if (!_id) {
      return res.send({ code: 200, msg: '缺少参数' });
    }
    await foodModel.remove({ _id });
    res.send({ code: 200, msg: '删除成功' });
  } catch (e) {
    res.send({ code: 200, msg: '删除失败' });
  }
});
/**
 * @api {post} /food/update 修改信息
 * @apiName update
 * @apiGroup Food
 *
 * @apiParam {String} us 用户名
 * @apiParam {String} price 价格
 * @apiParam {String} typename 类型名称
 * @apiParam {String} typeid 类型id
 * @apiParam {String} img 图片
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/update', async (req, res) => {
  try {
    let { name, price, desc, typename, typeid, img, _id } = req.body;
    if (!_id) {
      return res.send({ code: 200, msg: '缺少参数' });
    }
    let data = await foodModel.update(
      { _id },
      { name, price, desc, typename, typeid, img }
    );
    res.send({ code: 200, msg: '修改成功', data });
  } catch (e) {
    return res.send({ code: 200, msg: '修改失败' });
  }
});

/**
 * @api {post} /food/getInfoByPage 分页查询
 * @apiName getInfoByPage
 * @apiGroup Food
 *
 * @apiParam {String} pageSize 每页数据条数
 * @apiParam {String} page 哪一页
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/getInfoByPage', async (req, res) => {
  try {
    //let pageSize = req.body.pageSize || 2;// 现在都不这么写了吧
    let {pageSize=5,page=1} = req.body; // 解构的时候可以直接这么写.
    // let page = req.body.page || 1;

    let data = await foodModel
      .find()
      .limit(Number(pageSize))
      .skip(Number(page - 1) * pageSize);
    res.send({ code: 200, msg: '查询成功', data, data });
  } catch (e) {
    return res.send({ code: 200, msg: '查询失败' });
  }
});

module.exports = router;
